using System.Diagnostics.CodeAnalysis;
using System.Windows.Data;
using Bookvar.ObjectModel;
using Bookvar.UI.Converters;

namespace Bookvar.UI.ViewModel
{
    public partial class TopicViewModel : TopicViewModelBase
    {
        private bool isSelected;
        
        #region Properties

        [SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Used in bindings" )]
        public bool IsSelected
        {
            get { return isSelected; }
            set { SetFieldValue( "IsSelected", ref isSelected, value ); }
        }

        #endregion

        public TopicViewModel( Topic topic ) : base( topic )
        {
        }

        protected override void InitializeCommands()
        {
            base.InitializeCommands();

            Commands.Add( new NavigateToHyperlinkCommand( this ) );
        }

        public BindingBase CreateTopicPositionBinding()
        {
            Binding binding = new Binding("Resources") { Source = Topic };
            return binding;
        }

        /* event handler's logic

        private void AttachEventHandlers()
        {
            Topic.SubTopics.CollectionChanged += OnSubTopicsChanged;
            Topic.Attachments.CollectionChanged += OnAttachmentsChanged;
            Topic.PropertyChanged += OnTopicPropertyChanged;
        }

        private void DetachEventHandlers()
        {
            Topic.SubTopics.CollectionChanged -= OnSubTopicsChanged;
            Topic.Attachments.CollectionChanged -= OnAttachmentsChanged;
            Topic.PropertyChanged -= OnTopicPropertyChanged;
        }

        private void OnTopicPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Note")
            {
                if (Topic.Note != null)
                {
                    if (notesButton == null)
                    {
                        notesButton = new NotesButton();
                        notesButton.Topic = Topic;
                    }
                    if (notesAdorner == null && AdornerService != null)
                    {
                        notesAdorner = AdornerService.AdornTopic(
                            Topic, notesButton, AdornerPosition.TopRight, AdornerPlacement.OutHorizontally);
                    }
                }
                else
                {
                    if (notesAdorner != null)
                    {
                        if (AdornerService != null)
                        {
                            AdornerService.RemoveAdorner(notesAdorner);
                            notesAdorner = null;
                            notesButton = null;
                        }
                    }
                }
            }
            else if (e.PropertyName == "Hyperlink")
            {
                if (Topic.Hyperlink != null)
                {
                    if (hyperlinkButton == null)
                    {
                        hyperlinkButton = new HyperlinkButton();
                        hyperlinkButton.Topic = Topic;
                    }
                    if (hyperlinkAdorner == null && AdornerService != null)
                    {
                        hyperlinkAdorner = AdornerService.AdornTopic(
                            Topic, hyperlinkButton, AdornerPosition.TopRight, AdornerPlacement.OutVertically);
                    }
                }
                else
                {
                    if (hyperlinkAdorner != null)
                    {
                        if (AdornerService != null)
                        {
                            AdornerService.RemoveAdorner(hyperlinkAdorner);
                            hyperlinkAdorner = null;
                            hyperlinkButton = null;
                        }
                    }
                }
            }
        }

        private void OnSubTopicsChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            ExpandChildren();

            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    {
                        foreach (Topic topic in e.NewItems)
                        {
                            TopicContentPresenter child = CreateSubTopicPresenter(e.NewStartingIndex, topic);
                            owner.SelectedTopic = child;
                        }
                        break;
                    }
                case NotifyCollectionChangedAction.Remove:
                    {
                        foreach (Topic topic in e.OldItems)
                        {
                            TopicContentPresenter deleted = DeleteSubTopicPresenter(topic);
                            if (deleted.parentPresenter != null)
                            {
                                owner.SelectedTopic = deleted.parentPresenter;
                            }
                        }
                        break;
                    }
                case NotifyCollectionChangedAction.Move:
                    {
                        break;
                    }
                case NotifyCollectionChangedAction.Replace:
                    {
                        break;
                    }
                case NotifyCollectionChangedAction.Reset:
                default:
                    {
                        break;
                    }
            }

            HasChildren = Children.Count > 0;

            owner.InvalidateTopicsArrange();
        }

        protected virtual void OnAttachmentsChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (Topic.Attachments.Count > 0)
            {
                if (attachmentButton == null)
                {
                    attachmentButton = new AttachmentButton();
                    attachmentButton.Attachments = Topic.Attachments;
                }
                if (attachmentAdorner == null && AdornerService != null)
                {
                    attachmentAdorner = AdornerService.AdornTopic(
                        Topic, attachmentButton, AdornerPosition.BottomRight, AdornerPlacement.OutHorizontally);
                }
            }
            else
            {
                if (attachmentAdorner != null)
                {
                    if (AdornerService != null)
                    {
                        AdornerService.RemoveAdorner(attachmentAdorner);
                        attachmentAdorner = null;
                        attachmentButton = null;
                    }
                }
            }
        }
         
         */
    }
}